<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MX_Controller extends Controller {

	var $application = '';			// prefiks aplikasi yang sedang diakses
	var $unit = '';					// unit aplikasi yang sedang diakses
	var $function = '';				// fungsi aplikasi yang sedang diakses
	var $referrer = '';
	var $bypass_authorization = false;

	function __construct()
	{	
		parent::__construct();
		
		// mengisi prefiks, unit dan fungsi aplikasi yang sedang diakses	
			$this->application = get_application($this);
			$this->unit = get_unit($this);
			$this->function = get_function($this);
										
		// mengambil key user yang sedang login dan rolenya
			$user = $this->common->current_user_key();
			$role = $this->common->current_role_key();

		// mencatat akses user		
			$uri = current_url();
			$access_date = unix_to_mysql(now());
			$ip_address = $this->input->ip_address();
			$user_agent = $this->input->user_agent();
			$log = array('user' => $user, 'role' => $role, 'uri' => $uri, 'access_date' => $access_date,
				'ip_address' => $ip_address, 'user_agent' => $user_agent);
			if ($this->config->item('log_access')) {
				$this->db->insert('sys_t_access_log', $log);
			}
			
		if ((!isset($this->bypass_authorization)) || (!$this->bypass_authorization)) { // if not bypassed by controller, check permission	
			$allowed = $this->common->check_permission($this->application, $this->unit, $this->function);			
		} else {
			$allowed = true;
		}

		// tampilkan pesan error jika tidak memiliki privilege untuk mengakses aplikasi			
		if (!$allowed) {					
			$data = array(
			'application' => $this->application,
			'application_title' => $this->common->get_application_description($this->application),
			'page' => 'Access Denied',
			'navigation' => '',
			'content' => $this->load->view('content/access_denied', '', true),
			);
	
			echo ($this->load->view('layout/default', $data, TRUE));
			exit;
		}
		
		$this->referrer = $this->input->post('__referrer');
		if (!$this->referrer) {		
			$this->referrer = $this->session->flashdata('referrer');
			if (!$this->referrer) {
				$this->referrer = $this->agent->referrer();
			}
		}
	}
	
}
/* End of file MY_Controller.php */
/* Location: ./system/application/libraries/MY_Controller.php */